package com.example.makerspace.manager;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.makerspace.dao.CompanyDAO;
import com.example.makerspace.domain.CompanyDO;
import com.example.makerspace.dto.*;
import com.example.makerspace.enums.ResultCodeEnum;
import com.example.makerspace.utils.FileUtils;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.example.makerspace.service.impl.AdminServiceImpl.managerMap;

@Component
public class CompanyManager extends BaseManager<CompanyDO, CompanyDTO>{

    private final static String COMPANY_PICTURE_PATH_PREFIX = "company/";

    private final static List<Map<String, String>> FILTER_PARAMS = Lists.newArrayList(
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "category").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build()
    );

    private final static List<Map<String, String>> FUZZY_QUERY_PARAMS = Lists.newArrayList(
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "company_name").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "keyword").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "company_desc").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build(),
            ImmutableMap.<String, String>builder().put(QUERY_FIELD, "meeting").put(QUERY_TYPE, QUERY_TYPE_MATCH_PHRASE).build()
    );

  private  final  static  String   testHttpPrefix="https://119.45.199.13:3035/static/";

   private  final  static  String  devHttpPrefix="https://nism.ytcata.com:443/static/";
    @Resource
    private CompanyDAO companyDAO;

    @PostConstruct
    public void init(){
        this.baseDAO = companyDAO;
        managerMap.put("company", this);
    }

    public Result addCompany(CompanyDTO companyDTO){
        CompanyDO oldCompanyDO = companyDAO.selectByCompanyName(companyDTO.getCompanyName());
        if(oldCompanyDO != null){
            return Result.error(ResultCodeEnum.COMPANY_EXIST);
        }
        CompanyDO companyDO = new CompanyDO();
        BeanUtils.copyProperties(companyDTO, companyDO);
        String imagePath = FileUtils.saveMultipartFile(companyDTO.getPicture(), FileUtils.staticPath + COMPANY_PICTURE_PATH_PREFIX + companyDTO.getCompanyName());
        companyDO.setPicture(imagePath);
        companyDAO.insert(companyDO);
        return Result.success();
    }

    public Result updateCompany(CompanyDTO companyDTO){
        CompanyDO companyDO = companyDAO.selectById(companyDTO.getId());
        if(companyDO == null){
            return Result.error(ResultCodeEnum.COMPANY_NOT_EXIST);
        }
        Optional.ofNullable(companyDTO.getCompanyName()).ifPresent(companyDO::setCompanyName);
        Optional.ofNullable(companyDTO.getKeyword()).ifPresent(companyDO::setKeyword);
        Optional.ofNullable(companyDTO.getCompanyDesc()).ifPresent(companyDO::setCompanyDesc);
        Optional.ofNullable(companyDTO.getCategory()).ifPresent(companyDO::setCategory);
        Optional.ofNullable(companyDTO.getMeeting()).ifPresent(companyDO::setMeeting);
        Optional.ofNullable(companyDTO.getPicture()).ifPresent(picture ->{
            FileUtils.deleteFile(companyDO.getPicture());
            String imagePath = FileUtils.saveMultipartFile(picture, FileUtils.staticPath + COMPANY_PICTURE_PATH_PREFIX + companyDTO.getCompanyName());
            companyDO.setPicture(imagePath);
        });
        return updateDO(companyDO);
    }

    @Override
    public PageResult list(PageRequest pageRequest){
        Map<String, Object> params = pageRequest.getParams();
        Pagination pagination = pageRequest.getPagination();
        String fuzzy = MapUtils.getString(params, "fuzzy");
        String category = MapUtils.getString(params, "category");
        if(StringUtils.isBlank(fuzzy) && StringUtils.isBlank(category)){
            return super.list(pageRequest);
        }
        return search(FILTER_PARAMS, category, FUZZY_QUERY_PARAMS, fuzzy, pagination, CompanyDO.class);
    }


    public CompanyDTO convert2DTO(CompanyDO companyDO){
        CompanyDTO  companyDTO= super.convert2DTO(companyDO);
        String picture = companyDO.getPicture();
        String briefPath = FileUtils.getBriefPath(picture);
        String path = devHttpPrefix+ briefPath;
        companyDTO.setPicturePath(path);
        return companyDTO;
    }
}
